import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

class Kanvas extends JPanel{
  private BufferedImage kanvasImage;
  public Kanvas(final Palette currentPalette, final BufferedImage kanvasImage){
    this.kanvasImage = kanvasImage;;
    final Kanvas thisKanvas = this;
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        if(currentPalette.hasCurrentPen()){
          currentPalette.getCurrentPen().setKanvasImage(kanvasImage);
          currentPalette.getCurrentPen().penDown(e.getX(),e.getY());
          thisKanvas.paintImmediately(thisKanvas.getBounds());
        }
      }
    });
    addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        if(currentPalette.hasCurrentPen()){
          currentPalette.getCurrentPen().penUp(e.getX(),e.getY());
          thisKanvas.paintImmediately(thisKanvas.getBounds());
        }
      }
    });
    addMouseMotionListener(new MouseAdapter() {
      public void mouseDragged(MouseEvent e) {
        if(currentPalette.hasCurrentPen()){
          currentPalette.getCurrentPen().penDrag(e.getX(),e.getY());
          thisKanvas.paintImmediately(thisKanvas.getBounds());
        }
      }
    });
  }
  public void paintComponent(Graphics g){
    g.drawImage(kanvasImage, 0, 0, null);
  }
  public Dimension getMinimumSize(){
    return new Dimension(kanvasImage.getWidth(),kanvasImage.getHeight());
  }
  public Dimension getPreferredSize(){
    return new Dimension(kanvasImage.getWidth(),kanvasImage.getHeight());
  }
  public Dimension getMaximumSize(){
    return new Dimension(kanvasImage.getWidth(),kanvasImage.getHeight());
  }
  
}
